package top.infopub.mgr.la.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.la.dao.OutLawyerAttDao;
import top.infopub.mgr.la.dao.OutLawyerDao;
import top.infopub.mgr.la.domain.OutLawyerDomain;
import top.infopub.mgr.la.service.OutLawyerService;
import top.infopub.mgr.la.vo.OutLawyerVo;
import top.infopub.model.la.OutLawyer;
import top.infopub.model.la.OutLawyerAtt;

import java.util.List;

@Service
public class OutLawyerServiceImpl extends GenericServiceImpl<OutLawyer, String> implements
		OutLawyerService {
    
    @Autowired
    private OrgInfoService orgInfoService;

	@Autowired
	private OutLawyerDao outLawyerDao;
	
	@Autowired
    private OutLawyerAttDao outLawyerAttDao;
    
    @Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<OutLawyer, String> getDao() {
		return outLawyerDao;
	}

    @Override
    public Page<OutLawyerDomain> selectByDomainWhere(Page<OutLawyerDomain> page, OutLawyerVo outLawyerVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OutLawyerDomain>)outLawyerDao.selectByDomainWhere(outLawyerVo);
    }

    @Override
    public Page<OutLawyerDomain> selectMyCheckByDomainWhere(Page<OutLawyerDomain> page, OutLawyerVo outLawyerVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OutLawyerDomain>)outLawyerDao.selectMyCheckByDomainWhere(outLawyerVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOutLawyer(OutLawyerVo outLawyer, List<OutLawyerAtt> outLawyerAtts) {
        outLawyerDao.insertById(outLawyer);
        
        if(CollectionUtils.isNotEmpty(outLawyerAtts)){
            outLawyerAttDao.insertByBatch(outLawyerAtts);
        }
        
        String srcBizTable = "LA_OUT_LAWYER";
        String branchCode_xm = "from_项目发起";
        String branchCode_super = "from_总部发起";
        String hrOrgId = outLawyer.getHrOrgId();// 申请公司
        // 公司类型 1=总部公司 2=区域公司 3=项目公司
        String type = orgInfoService.selectByPrimaryKey(hrOrgId).getTypes();
        String templateId = "";
        // 总部发起 
        if ("1".equals(type)) {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_super);
        }
        else {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_xm);
        }

        // 处理工作流数据
        wsFlowProcess.launch(templateId, srcBizTable, outLawyer.getId(), outLawyer.getProcessId(), null);
    }

    @Override
    public OutLawyerVo queryById(String id) {
        return outLawyerDao.selectById(id);
    }

}